package com.lk.controller;

import com.google.common.collect.Lists;
import com.lk.listener.UserExcelListener;
import com.lk.model.User;
import com.lk.utils.EasyExcelUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: Li Kang
 * @create: 2020-05-04 15:01
 */
@Controller
public class TestController {

    /**
     * 导出测试
     * @param response
     * @throws Exception
     */
    @GetMapping("/exportTest")
    void exportTest(HttpServletResponse response) throws Exception {
        //单sheet,单table导出测试
        List<User> userList = Lists.newArrayList();
        for (int i = 0; i < 5 ; i++) {
            User user = new User("admin"+i,"123"+i,1+i,new Date());
            userList.add(user);
        }
        EasyExcelUtil.exprotExcel(response,userList,"导出测试","sheet单1",User.class);
    }


    /**
     * 上传测试
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("upload")
    @ResponseBody
    public String upload(MultipartFile file) throws IOException {
//        EasyExcel.read(file.getInputStream(), User.class, new UploadDataListener(uploadDAO)).sheet().doRead();
        EasyExcelUtil.asyncReadModel(file.getInputStream(),new UserExcelListener<>(),User.class,0,0);
        return "success";
    }

}

